<div id="admin_list">
<!--显示所有的管理员列表-->
<h3>管理员信息列表</h3>
<table>
	 <tr>
		<th>编号</th>
		<th>用户名</th>
		<th>权限</th>
		<th>授权时间</th>
		<th>最后登录时间</th>
		<th>最后登陆IP</th>
		<th class="span-2">操作</th>
	 </tr>
<?php
if( !empty( $admins ) ){
foreach( $admins as $no=>$admin ){
?>	 
<tr>
	 <td class="align_center" class="admin_id"><?php echo $admin['admin_id'];?></td>
	 <td class="align_center admin"><?php echo $admin['admin'];?></td>
	 <td class="align_center priv"><?php echo $admin['priv'];?></td>
	 <td class="align_center"><?php echo $admin['reg_time'];?></td>
	 <td class="align_center"><?php echo $admin['last_login_time'];?></td>
	 <td class="align_center"><?php echo $admin['last_login_ipadd'];?></td>
	 <td class="align_center"><span class="button edit">修改</span><span class="button del">删除</span></td>	
</tr>
<?php
}
}else{
?>

<?php
}
?>
</table>
</div>

<div id="dialog-form" title="" style="display:none;">
	<form>
	<fieldset>
		<!--input type="hidden" id="product_id" name="product_id" value=""/-->
		<p>
			<label for="admin">管理员帐号(不能为空)</label><br />
			<input type="text" name="admin" class="text ui-widget-content ui-corner-all" id="admin"/>
		</p>
		<p>
			<label for="password">密码</label><br />
			<input type="text" name="password" id="password" class="text ui-widget-content ui-corner-all" />
		</p>
		<p>
			<label for="priv">权限</label><br />
			<select id="priv" name="priv">
				<option value="COMMON">普通管理员</option>
				<option value="SUPER">超级管理员</option>
			</select>
		</p>
	</fieldset>
	</form>
</div>

<script type="text/javascript">
$( '.button' ).each( function( index , el ){
	 var this_button_el = $( el );
	 var this_tr_el = this_button_el.parent().parent();
	 var admin_id = this_tr_el.find( '.admin_id' ).text();
	 if( this_button_el.hasClass( 'del' ) ){
		 this_button_el.button({
			 icons: {
				 primary: "ui-icon-close"
			 },
			 text: false
		 });
		 this_button_el.click( function(){
			 //执行删除操作
			 $.post( '/admin/ajax_priv_do_del/' , { "admin_id":admin_id } , function( res ){
                                 check_ajax_res_priv_err( res );
				 if( res.res == "TRUE" ){
					 this_tr_el.fadeOut();	 
				 }else{
					 throw new Error( "删除管理员时出错,AJAX返回FALSE" );
				 }
			 } , "json" );
		 });
	 }
	 if( this_button_el.hasClass( 'edit' ) ){
		 this_button_el.button({
			 icons: {
				 primary: "ui-icon-gear"
			 },
			 text: false
		 });
		 this_button_el.click( function(){
			 //获取默认信息
			 var admin_td_el = this_tr_el.find( '.admin' );
			 var priv_td_el = this_tr_el.find( '.priv' );
			 admin = admin_td_el.text();
			 priv = priv_td_el.text();
			 //用默认信息填充表单
			 var admin_to_update_input_el = $( 'input[name=admin]' );
			 var priv_to_update_input_el = $( '#priv' );
			 var password_to_update_input_el = $( 'input[name=password]' );
			 admin_to_update_input_el.val( admin );
			 priv_to_update_input_el.val( priv );
			 var this_dialog_form_el = $( "#dialog-form" );
			 this_dialog_form_el.dialog( "destroy" );
			 this_dialog_form_el.dialog({
				 modal: true ,
				 width: 480 ,
				 height: 350 ,
				 buttons: {
					 "提交": function(){
						 //判断数据有效性
						 if( admin_to_update_input_el.val().length == 0 ){
							 admin_to_update_input_el.addClass( 'ui-state-error' );
							 admin_to_update_input_el.focus( function(){
								 admin_to_update_input_el.removeClass( 'ui-state-error' );
							 });
							 return;
						 }
						 $.post( '/admin/ajax_priv_do_update/' , 
						 { 
							 "admin_id":admin_id , 
							 "admin":admin_to_update_input_el.val() , 
							 "priv": priv_to_update_input_el.val() ,
							 "password": password_to_update_input_el.val()
                                                 } , function( res ){
                                                         check_ajax_res_priv_err( res );
							 if( res.res == "TRUE" ){
								this_dialog_form_el.dialog( "destroy" );
								//刷新信息
								admin_td_el.text( admin_to_update_input_el.val() );
								priv_td_el.text( priv_to_update_input_el.val() );
							 }else{
                                                                this_dialog_form_el.dialog( "destroy" );
								throw new Error( "更新管理员信息时,AJAX返回FALSE" );
							 }
						 } , "json" );
					 },
					 "取消": function(){
					 	this_dialog_form_el.dialog( "destroy" );
					 }
				 }
			 });
		 });
	 }
});
</script>
